<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>动态修改属性</title>
</head>
<body>
<div id="change" class="change">
    <img data-bind:[attribute_name]="attribute_value" alt="" />
</div>


<script>
    let attribute_name = "src"
    let attribute_value = "https://consumer.huawei.com/content/dam/huawei-cbg-site/common/mkt/pdp/phones/mate-x3/img/kv/kv@2x.webp"

    let element = document.querySelector("#change img");
    // console.log(element);

    let attributeNames = element.getAttributeNames();

    for (let i = 0; i < attributeNames.length; i++) {
        // 包含动态绑定属性
        if((attributeNames[i].indexOf("data-bind:") !== -1) ||(attributeNames[i].indexOf(":") === 0 ) ) {
            console.log(attributeNames[i]);

            let result = attributeNames[i].substring(attributeNames[i].indexOf(':') + 1);
            // console.log(result);
            let name = eval(result)[0]
            console.log(name);

            let value = eval(element.getAttribute(attributeNames[i]));

            element.setAttribute(name, value)
            element.removeAttribute(attributeNames[i])
        }
    }
</script>

</body>
</html>